package com.dingreading.cloud.soms.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.dto.FollowTypeDto;
import com.dingreading.cloud.soms.entity.FollowType;
import com.dingreading.cloud.soms.service.FollowTypeService;
import com.dingreading.cloud.soms.service.MemberFollowService;
import com.dingreading.cloud.soms.util.FollowUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/front/followType")
@Api(tags = "门店-跟进类型管理")
public class FrontFollowTypeController {

    @Resource
    MemberFollowService memberFollowService;
    @Resource
    FollowTypeService followTypeService;

    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList")
    public R<Object> pageList(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "跟进类型名称") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "禁用状态 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) throws Exception {
        Page<FollowTypeDto> page = this.followTypeService.pageList(pageUtil, storeUid, keyword, enabled);

        PageBeanDto dto = new PageBeanDto();
        dto.setList(page.getRecords());
        dto.setTotal(page.getTotalRow());
        return R.ok(dto);
    }

    @ApiOperation(value = "获取门店的跟进类型(包含默认类型)")
    @PostMapping(value = "/getFollowType", produces = "application/json; charset=UTF-8")
    public R<Object> getFollowType(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName
    ) {
        List<FollowTypeDto> list = followTypeService.getByStoreUid(storeUid);
        return R.ok(list);
    }

    @ApiOperation(value = "获取本门店的跟进类型(不包含默认类型)")
    @PostMapping(value = "/getStoreFollowType", produces = "application/json; charset=UTF-8")
    public R<Object> getStoreFollowType(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName
    ) {
        List<FollowTypeDto> list = followTypeService.getStoreFollowType(storeUid);
        return R.ok(list);
    }

    @ApiOperation(value = "创建/编辑跟进类型")
    @PostMapping(value = "/newFollowType", produces = "application/json; charset=UTF-8")
    public R<Object> newFollowType(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "跟进类型uid") @RequestParam(value = "uid", required = false) String uid,
            @ApiParam(value = "跟进类型名称") @RequestParam(value = "name") String name
    ) {
        FollowType followType = null;
        if (StringUtils.isNotBlank(uid)) {
            long haveName = followTypeService.getHaveName(storeUid, uid, name);
            if (haveName > 0) return R.fail("名称不能重复");

            followType = followTypeService.getBean(storeUid, uid);
            if (null == followType) return R.fail("跟进类型获取失败");

        } else {
            long haveName = followTypeService.getHaveName(storeUid, null, name);
            if (haveName > 0) return R.fail("名称不能重复");
        }

        FollowType data = FollowUtil.newFollowType(
                followType, corpUid, storeUid, staffUid, staffName, name, 1
        );
        followTypeService.saveOrUpdate(data);
        return R.ok();
    }

    @ApiOperation(value = "删除跟进类型")
    @PostMapping(value = "/dropFollowType", produces = "application/json; charset=UTF-8")
    public R<Object> dropFollowType(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "跟进类型id") @RequestParam(value = "uid") String uid
    ) {
        boolean i = memberFollowService.haveFollowTypeUid(storeUid, uid);
        if (i) return R.fail("已关联跟进记录，不能操作");

        int l = followTypeService.dropBean(storeUid, uid);
        if (l <= 0) return R.fail("删除失败");

        return R.ok();
    }
}